import Vue from 'vue'
import VueRouter from 'vue-router'
import Cookies from 'js-cookie'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue'),
    meta: { requiresAuth: false }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('@/views/Register.vue'),
    meta: { requiresAuth: false }
  },
  {
    path: '/403',
    name: 'Error403',
    component: () => import('@/views/Error403.vue'),
    meta: { requiresAuth: false }
  },
  {
    path: '/',
    component: () => import('@/layout/MainLayout.vue'),
    redirect: '/home',
    meta: { requiresAuth: true },
    children: [
      {
        path: 'home',
        name: 'Home',
        component: () => import('@/views/Home.vue'),
        meta: { title: '首页', icon: 'el-icon-s-home' }
      },
      {
        path: 'processes',
        name: 'Processes',
        component: () => import('@/views/Process/ProcessList.vue'),
        meta: { title: '流程管理', icon: 'el-icon-setting' }
      },
      {
        path: 'process/create',
        name: 'ProcessCreate',
        component: () => import('@/views/Process/ProcessForm.vue'),
        meta: { title: '创建流程' }
      },
      {
        path: 'process/edit/:id',
        name: 'ProcessEdit',
        component: () => import('@/views/Process/ProcessForm.vue'),
        meta: { title: '编辑流程' }
      },
      {
        path: 'approval/select',
        name: 'ApprovalSelect',
        component: () => import('@/views/Approval/ApprovalSelect.vue'),
        meta: { title: '发起审批', icon: 'el-icon-plus' }
      },
      {
        path: 'approval/start/:id',
        name: 'ApprovalStart',
        component: () => import('@/views/Approval/ApprovalStart.vue'),
        meta: { title: '填写审批' }
      },
      {
        path: 'approval-center',
        name: 'ApprovalCenter',
        component: () => import('@/views/Approval/ApprovalCenter.vue'),
        meta: { title: '审批中心', icon: 'el-icon-s-grid' }
      },
      {
        path: 'approval/detail/:id',
        name: 'ApprovalDetail',
        component: () => import('@/views/Approval/ApprovalDetail.vue'),
        meta: { title: '审批详情' }
      },
      // 系统管理
      {
        path: 'system/overview',
        name: 'SystemOverview',
        component: () => import('@/views/System/SystemOverview.vue'),
        meta: { 
          title: '系统概览', 
          icon: 'el-icon-monitor',
          permission: 'system:overview'
        }
      },
      {
        path: 'system/users',
        name: 'UserManagement',
        component: () => import('@/views/System/UserManagement.vue'),
        meta: { 
          title: '用户管理', 
          icon: 'el-icon-user',
          permission: 'system:user:list'
        }
      },
      {
        path: 'system/roles',
        name: 'RoleManagement',
        component: () => import('@/views/System/RoleManagement.vue'),
        meta: { 
          title: '角色管理', 
          icon: 'el-icon-s-custom',
          permission: 'system:role:list'
        }
      },
      {
        path: 'system/permissions',
        name: 'PermissionManagement',
        component: () => import('@/views/System/PermissionManagement.vue'),
        meta: { 
          title: '权限管理', 
          icon: 'el-icon-key',
          permission: 'system:permission:list'
        }
      },
      {
        path: 'system/auth',
        name: 'AuthManagement',
        component: () => import('@/views/System/AuthManagement.vue'),
        meta: { 
          title: '授权管理', 
          icon: 'el-icon-s-check',
          permission: 'system:auth:user-role'
        }
      }
    ]
  }
]

const router = new VueRouter({
  mode: 'hash',
  routes
})

// 路由守卫
router.beforeEach(async (to, from, next) => {
  const token = Cookies.get('token')
  
  if (to.matched.some(record => record.meta.requiresAuth !== false)) {
    if (!token) {
      next('/login')
    } else {
      // 检查权限
      if (to.meta.permission) {
        const hasPermission = await checkPermission(to.meta.permission)
        if (!hasPermission) {
          next('/403') // 权限不足页面
          return
        }
      }
      next()
    }
  } else {
    if (token && to.path === '/login') {
      next('/')
    } else {
      next()
    }
  }
})

// 权限检查函数
async function checkPermission(permission) {
  try {
    // 从localStorage获取用户权限列表
    const userPermissions = JSON.parse(localStorage.getItem('userPermissions') || '[]')
    return userPermissions.includes(permission)
  } catch (error) {
    console.error('权限检查失败:', error)
    return false
  }
}

export default router

